var g_lastId = -1;  //全局最后ID号
var g_max_length = 500;  //显示最大行数,超过的会被移除掉
var g_run_log_falg = true;
var g_log_count = 0;

$(function () {
    //获取所有应用的数据
    getAllApp($("#appCode"));
    //初始化页面元素
    initPageUiEvent();
});


//初始化页面元素的事件
function initPageUiEvent() {
    //应用下拉框的选择事件
    $("#appCode").combobox({
        onChange: function (newValue,oldValue) {
            $.cookie('logPage_appCode', newValue, { expires: 30 });
            //获取所有节点的数据
            getAllNode($("#nodeCode"),newValue);
        }
    });

    //下拉框加载事件
    $("#level").combobox({
        onLoadSuccess: function (data) {
            var cookieLevel = $.cookie('logPage_level');
            if (typeof cookieLevel != "undefined"){
                for(var i =0 ;i<data.length;i++){
                    if(data[i].value==cookieLevel){
                        $("#level").combobox('select', data[i].value);
                    }
                }
            }else{
                $("#level").combobox('select', data[1].value);
            }
        },
        onChange: function (newValue,oldValue) {
            $.cookie('logPage_level', newValue, { expires: 30 });
        }
    });

    //清空事件
    $("#clearBtn").click(function () {

        $("#log_list_div").empty();
        g_log_count = 0;
    });
    //暂停事件
    $("#pauseBtn").click(function () {
        var app_code = $("#appCode").combobox("getValue");
        var node_code = $("#nodeCode").combobox("getValue");
        var level = $("#level").combobox("getValue");
        if (app_code == "" || node_code == ""){
            return;
        }
        var tag = $(this).linkbutton("options").text;
        if (tag == "暂停"){
            $(this).linkbutton({
                text: "开始",
                selected:true,
                iconCls:'icon-play'
            });
            g_run_log_falg = false;
        }else{
            $(this).linkbutton({
                text: "暂停",
                selected:false,
                iconCls:'icon-pause'
            });
            g_run_log_falg = true;
            getLastId();
        }
    });
    //设置显示区域的高度
    var divHeight = document.body.clientHeight-(55);
    // $("#log_list_div").prop("style","height:"+divHeight+"px");
    $("#log_list_div").attr("style","height:"+divHeight+"px");

    //设置显示日志信息的弹框
    $("#logDialog-dlg").dialog({
        title:'日志详细信息',
        width: $(window).width()*0.7,
        height: $(window).height()*0.7,
        closed: true,
        cache: false,
        shadow: false,
        modal: true,
        buttons: [{
            text: '关闭',
            iconCls: 'icon-save',
            handler: function () {
                $("#logDialog-dlg").dialog('close');
            }
        }],
        onClose: function () {
            //关闭对话框事件
        },
    });
}


//获取最大的记录ID
function getLastId() {
    if (g_msg != ""){
        return;
    }
    var app_code = $("#appCode").combobox("getValue");
    var node_code = $("#nodeCode").combobox("getValue");
    var task_code = $("#taskCode").combobox("getValue");
    var level = $("#level").combobox("getValue");
    if (app_code == "" || node_code == ""){
        return;
    }
    $("#log_list_div").empty();
    g_log_count = 0;
    $.ajax({
        type: 'post',
        url: contextPath + '/console/log/getLogLastId',
        data: {
            app_code : app_code,
            node_code: node_code,
            task_code: task_code,
            level :level
        },
        success: function (result) {
            var obj = JSON.parse(result);
            g_lastId = parseInt(obj.id,10);
            realTimeLogs();
        },
        error: function (xhr, status, error) {
            apexLayer.Warn('调用服务失败');
        }
    });
}

/**
 * 时实读取日志
 */
function realTimeLogs() {
    //如果有提示信息,则不读取日志
    if (g_msg != ""){
        return;
    }
    console.log("开始读取");
    var app_code = $("#appCode").combobox("getValue");
    var node_code = $("#nodeCode").combobox("getValue");
    var level = $("#level").combobox("getValue");
    var task_code = $("#taskCode").combobox("getValue");
    //console.log(app_code+"|"+node_code);
    if (app_code == "" || node_code == ""){
        return;
    }
    $.ajax({
        type: 'post',
        url: contextPath + '/console/log/getRealTimeLog',
        data: {
            app_code : app_code,
            node_code: node_code,
            id       : g_lastId,
            task_code: task_code,
            level    : level
        },
        success: function (result) {
            //console.log(result);
            if (result != ""){
                var obj = JSON.parse(result);
                var array = obj.root;
                if (array.length != 0){
                    for (var i=0;i<array.length;i++){
                        //<div class="alert alert-success" role="alert">...</div>
                        var str = "<li class='"+array[i].level+"'>"
                            + "<span class='icon' onclick='showContent(this)';></span>"
                            // + "[ "+array[i].level+" ]  "
                            // +"appCode:"+array[i].appCode+" appName:"+array[i].appName
                            // +" nodeCode:" + array[i].nodeCode+ " nodeName:"+array[i].nodeName +" "
                            // + array[i].date + " "+ array[i].time + " <br>"
                            +array[i].id + "  |   "
                            +array[i].content +"</li>";
                        $("#log_list_div").append(str);
                        g_log_count++;
                        if (g_log_count>g_max_length){
                            $("#log_list_div").find("li").get(0).remove();
                        }
                    }
                    //移掉多余的记录
                    //定位滚动条
                    $("#log_list_div").scrollTop($("#log_list_div")[0].scrollHeight);
                    //记录ID更新
                    g_lastId = array[array.length-1].id;
                    //延迟执行递归方法
                }
                if (g_run_log_falg){
                    setTimeout(function () {
                        realTimeLogs();
                    },1000);
                }
            }
        },
        error: function (xhr, status, error) {
            apexLayer.Warn('调用服务失败');

        }
    });
}


//显示日志内容
function showContent(obj){
    $("#showDialog").text($(obj).parent().text());
    //$(obj).parent().text()
    $("#logDialog-dlg").dialog('open');
}

